package com.juzhi.dvrapp.manager;

import android.content.Context;

import com.juzhi.dvrapp.bean.Device;
import com.juzhi.dvrapp.bean.DeviceModel;
import com.juzhi.dvrapp.utils.Config;
import com.juzhi.dvrapp.utils.GreenDaoUtil;
import com.juzhi.dvrapp.utils.MLog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YangHu on 2017/8/31.
 */

public class DeviceManager {

    //设备列表
    private List<Device> deviceList = new ArrayList<>();
    //上下文
    private Context context;

    //单例模式
    private static DeviceManager instance;

    public static DeviceManager getDeviceManager(Context context) {
        if (instance == null)
            instance = new DeviceManager(context);
        return instance;
    }

    private DeviceManager(Context context) {
        this.context = context;
        MLog.e("DeviceManager 创建-----");

    }

    /**
     * 初始化
     */
    public void initialize() {
        List<DeviceModel> list = GreenDaoUtil.queryDeviceList();
        deviceList.clear();
        MLog.i("initialize---数据清空");
        if (list.size() > 0) {
            for (DeviceModel model : list) {
                //可以在此对device进行一些操作
                if (model.getDeviceType() == 1) {  //远程设备
                    Device device = new Device();
                    device.setDeviceModel(model);
                    device.setStatus(Config.DEVICE_RESULT_CONNECTING);
                    deviceList.add(device);
                    device.createDevice();
                } else {  //本地设备

                }
            }
        }
    }

    /**
     * 添加设备
     *
     * @param device
     */
    public void saveDevice(Device device) {
        deviceList.add(device);
        device.connect();
        GreenDaoUtil.insertDevice(device.getDeviceModel());
    }

    /**
     * 删除设备
     */
    public void deleteDevice(Device device) {
        deviceList.remove(device);
        GreenDaoUtil.deleteDevice(device.getDeviceModel());
    }

    /**
     * 获取设备对象
     *
     * @param did
     * @return
     */
    public Device getDevice(String did) {
        for (Device device : deviceList) {
            if (device.getDeviceModel().getSsid().equals(did)) {
                return device;
            }
        }
        return null;
    }

    public Device getLocalDevice(String did) {
        for (Device device : deviceList) {
            if (device.getDeviceModel().getDeviceType() == 0) {
                return device;
            }
        }
        return null;
    }

    /**
     * 设备用户信息管理
     */
    public void setDeviceSystemUser(long userid, String jsonObj) {

    }

    /**
     * 返回设备列表
     *
     * @return
     */
    public List<Device> getDeviceList() {
        MLog.i(deviceList.size());
        return deviceList;
    }

    /**
     * 返回本地设备
     */
    public Device getLocalDevice() {
        for (Device device : deviceList) {
            if (device.getDeviceModel().getDeviceType() == 0)
                return device;
        }
        return null;
    }

    /**
     * 返回远程设备
     */
    public List<Device> getRemoteDevice() {
        List<Device> remoteDevice = new ArrayList<>();
        for (Device device : deviceList) {
            if (device.getDeviceModel().getDeviceType() == 1)
                remoteDevice.add(device);
        }
        return remoteDevice;
    }

    public void connectionDevice() {
        for (Device device : deviceList) {
            if (device.getDeviceModel().getDeviceType() == 1) {
                device.setStatus(Config.DEVICE_RESULT_CONNECTING);
                device.createDevice();
            }
        }
    }
}
